package req

import (
	"io"
	"logbeat/internal/ectx"
	"net/http"
)

func Req(ectx *ectx.Ectx, method string, url string,body io.Reader) (rep string,status int,err error) {
	req, err := http.NewRequest(method, url, body)
	if err!=nil{
		return "",0,err
	}
	req.SetBasicAuth(ectx.Config.Beat.Username, ectx.Config.Beat.Password)
	req.Header.Set("Content-Type", "application/json")
	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		return "",0,err
	}
	defer resp.Body.Close()

	// log.Println(resp.StatusCode)
	b, err := io.ReadAll(resp.Body)
	if err != nil {
		return "",resp.StatusCode,err
	}
	return string(b),resp.StatusCode,nil
}
 
